// top_crawler.js
import puppeteer from 'puppeteer';
import mysql from 'mysql2/promise';
import axios from 'axios';

// 数据库配置
const dbConfig = {
    host: 'localhost',
    user: 'root',
    password: '123456',  // 如果你设置了密码，请填入
    database: 'musiccenter'
};

// QQ音乐排行榜API
const API_URL = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_toplist_cp.fcg';

async function crawlTopList() {
    // 创建数据库连接
    const connection = await mysql.createConnection(dbConfig);

    try {
        // 创建数据表
        await connection.execute(`
            CREATE TABLE IF NOT EXISTS hotranking (
                songID INT AUTO_INCREMENT PRIMARY KEY,
                songtitle VARCHAR(255) NOT NULL,
                songauthor VARCHAR(255) NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
        `);

        // 构造请求参数（以热歌榜为例）
        const params = {
            tpl: 3,
            page: 'detail',
            date: new Date().toISOString().slice(0, 10).replace(/-/g, ''),
            topid: 26,  // 26为热歌榜ID
            type: 'top',
            song_begin: 0,
            song_num: 100,
            g_tk: 5381,
            loginUin: 0,
            hostUin: 0,
            format: 'json',
            inCharset: 'utf8',
            outCharset: 'utf-8',
            notice: 0,
            platform: 'yqq.json',
            needNewCode: 1
        };

        // 发送API请求
        const response = await axios.get(API_URL, {
            params,
            headers: {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
                'Referer': 'https://y.qq.com/n/ryqq/toplist'
            }
        });

        // 处理数据
        const songList = response.data.songlist || [];
        const insertData = songList.map(song => [
            song.data.name,  // 歌曲名称
            song.data.singer.map(s => s.name).join('/')  // 多个歌手用斜杠分隔
        ]);

        // 检查 insertData 是否为空
        if (insertData.length === 0) {
            console.warn('没有有效的歌曲数据，跳过插入操作');
            return;
        }

        // 批量插入数据（自动忽略重复）
        const [result] = await connection.query(
            `INSERT IGNORE INTO hotranking (songtitle, songauthor) VALUES ?`,
            [insertData]
        );

        console.log(`成功插入 ${result.affectedRows} 条数据`);
    } catch (error) {
        console.error('爬取失败:', error.message);
    } finally {
        await connection.end();
    }
}

crawlTopList();
